
import { _decorator, Component, Node, input, Input, EventMouse, Vec2, SystemEvent, EventTouch, SystemEventType } from 'cc';
import { Main } from '../Main';
const { ccclass, property } = _decorator;


  
@ccclass('MouseInput')
export class MouseInput extends Component {
    private _mouseDown=false
	private _startMove=false
	private _tempVec=new Vec2()
	startPos=new Vec2(0,0)
	mousePos=new Vec2(0,0)
	private _dir=null
	scaleX=1
	scaleY=1
	
    init () {
		// this.scaleX = window.innerWidth / window.devicePixelRatio/Main.I.screenWidth
		// this.scaleY = window.innerHeight / window.devicePixelRatio/Main.I.screenHeight
		
		this.node.on(SystemEventType.TOUCH_START,(event:EventTouch)=>{
			this._mouseDown=true
			this._startMove=false
			event.getUILocation(this.startPos)
			event.getUILocation(this.mousePos)
		},this)
		this.node.on(SystemEventType.TOUCH_END,(event:EventTouch)=>{
			this._mouseDown=false
			this._startMove=false
		})
		this.node.on(SystemEventType.TOUCH_CANCEL,(event:EventTouch)=>{
			this._mouseDown=false
			this._startMove = false
		})
		this.node.on(SystemEventType.MOUSE_MOVE,(event:EventTouch)=>{
			if(!this._mouseDown) return
			event.getUILocation(this.mousePos)
		})


    }
	loop(){
		this._dir=null
		if(!this._mouseDown) return

		Vec2.subtract(this._tempVec, this.mousePos, this.startPos)
		
		if(!this._startMove){
			if(Vec2.len(this._tempVec)>2){
				this._startMove=true
			}
		}
		
		if(this._startMove){
			this._dir = this._tempVec
			Vec2.normalize(this._dir, this._dir)
		}
		
	}
	
	getDir(){
		let res=this._dir
		return res
	}
	
	transformMousePos(pos){
		pos.x=pos.x*this.scaleX
		pos.y=pos.y*this.scaleY
	}
}
